package com.tangbao.news.service.impl;

import com.tangbao.news.mapper.OneNewsMapper;
import com.tangbao.news.model.OneNews;
import com.tangbao.news.service.OneNewsService;
import com.tangbao.util.Page;
import com.tangbao.util.ResEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * 功能:
 * 作者: zhouxin
 * 日期: 2018/3/2
 */
@Service
public class OneNewsServiceImpl implements OneNewsService {
    @Resource
    OneNewsMapper oneNewsMapper;

    /**
     * 分页查询全部一级菜单
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Response getOneNewsByAll(Integer currentPage, Integer pageSize) {
        Page page = new Page();
        if(null != currentPage) {
            page.setCurrentPage(currentPage);
        }
        if(null != pageSize) {
            page.setPageSize(pageSize);
        }

        Integer totalCount = oneNewsMapper.getOneNewsNum();
        page.setTotalCount(totalCount);

        List<OneNews> allOneNewss = oneNewsMapper.getOneNewsByAll(page.getStart(),pageSize);
        page.setList(allOneNewss);
        ResEntity entity = ResEntity.okEntity().put("page", page);
        return Response.ok(entity).build();
    }

    @Override
    public Response getOneNewsById(String id) {
        OneNews oneNewss = oneNewsMapper.getOneNewsById(Integer.parseInt(id) );
        ResEntity entity = ResEntity.okEntity().put("oneNewss", oneNewss);
        return Response.ok(entity).build();
    }
    @Override
    public Response saveOneNews(OneNews oneNews) {
        int size = oneNewsMapper.saveOneNews(oneNews);
        ResEntity entity = ResEntity.okEntity().put("size", size);
        return Response.ok(entity).build();
    }
    @Override
    public Response updateOneNews(OneNews oneNews) {
        int size = oneNewsMapper.updateOneNews(oneNews);
        ResEntity entity = ResEntity.okEntity().put("size", size);
        return Response.ok(entity).build();
    }
    @Override
    public Response updateOneNewsClick(Integer id) {
        int size = oneNewsMapper.updateOneNewsClick(id);
        ResEntity entity = ResEntity.okEntity().put("size", size);
        return Response.ok(entity).build();
    }

}
